package CardGame;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardDemo {

    public static final String[] SUITS = {"♥","♠","♦","♣"};

    //扑克牌
    public static List<Card> newCard() {
        List<Card> cardList = new ArrayList<>();
        for (int i = 0; i < SUITS.length; i++) {
            for (int j = 1; j <= 13; j++) {
                Card card = new Card(SUITS[i], j);
                cardList.add(card);
            }
        }
        return cardList;
    }

    //洗牌
    public static void shuffle(List<Card> cardList) {
        for (int i = 0; i < cardList.size(); i++) {
            Random random = new Random();
            int index = random.nextInt(cardList.size());
            swap(cardList,i,index);
        }    
    }

    //数组元素随机交换
    public static void swap(List<Card> cardList,int x,int y) {
        Card tmp = cardList.get(x);

        cardList.set(x,cardList.get(y));

        cardList.set(y,tmp);

    }



    public static void main(String[] args) {
        List<Card> cardList = newCard();
        System.out.println(cardList);

        System.out.println("------------------");
        System.out.println("洗牌");

        shuffle(cardList);
        System.out.println(cardList);

        System.out.println("------------------");
        System.out.println("摸牌");

        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hands = new ArrayList<>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cardList.remove(0);
                hands.get(j).add(card);
            }
        }

        System.out.println("1号的牌：");
        System.out.println(hand1);

        System.out.println("2号的牌：");
        System.out.println(hand2);

        System.out.println("3号的牌：");
        System.out.println(hand3);

        System.out.println("剩余的牌：");
        System.out.println(cardList);

    }
}
